package com.fadis.utils;

import com.fadis.cmd.FadisActionFrame;

import java.util.ArrayList;
import java.util.List;

/**
 * set key value  转成  RedisFrame
 * 但是要注意，命令行的形式是string[]，你不需要处理字符串形式
 */
public class CliParser {


    public FadisActionFrame parseCli(String[] args){

        FadisActionFrame frame = new FadisActionFrame();
        for (String arg: args){
            frame.getArgs().add(arg);
        }

        return frame;
    }

    public FadisActionFrame parseCli(String cli){

        boolean hasDoubleQuote = false;
        String currentArg = "";
        List<String> args = new ArrayList<>();

        for (int i = 0; i < cli.length(); i++) {
            char c = cli.charAt(i);
            if(c == '"'){
                System.out.println("碰见引号了--" + i);
                if(!hasDoubleQuote){
                    // 双引号开始
                    hasDoubleQuote = true;
                }else{
                    //双引号结束
                    hasDoubleQuote = false;
                }
            }else if(c == ' ' && !hasDoubleQuote){
                if(currentArg.length() > 0){
                    args.add(currentArg);
                    currentArg = "";
                }else{
                    // 直接忽略多余的空格
                }

            }else{
                currentArg += c;
            }
        }

        if(!hasDoubleQuote && currentArg.length() > 0){
            args.add(currentArg);
            currentArg = "";
        }

        FadisActionFrame frame = new FadisActionFrame();
        frame.getArgs().addAll(args);
        return frame;
    }

}
